package com.m3958.logcollector.controller;

import java.util.Map;

import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.m3958.logcollector.tree.TreeHolder;
import com.m3958.logcollector.tree.TreeUpdateAction;

@Controller
public class TreeController {
    
    @Autowired
    @Named("mapValued")
    private TreeHolder<Map<String, String>> treeHolder;

    @ExceptionHandler
    public ResponseEntity<String> exceptionHander(Exception exp) {
        exp.printStackTrace();
        return ResponseEntity.ok("");
    }

    @RequestMapping(value = "/treenodes/{id}/update", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<TreeUpdateAction<Map<String, String>>> getTree(@PathVariable Long id, @RequestBody TreeUpdateAction<Map<String, String>> tua) {
        tua = treeHolder.doUpdate(id, tua);
        return ResponseEntity.ok(tua);
    }
}
